package com.xiaow.springsecuriydemo.vo;

import com.xiaow.springsecuriydemo.utils.SpringContextUtil;
import org.apache.catalina.core.ApplicationContext;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * 做定时器的任务 这里是做秒杀的业务
 *
 * @ClassName MyJob
 * @Author xiaow
 * @DATE 2022/9/18 19:07
 **/

@DisallowConcurrentExecution   //设置之后表示不允许多线程进行
@PersistJobDataAfterExecution   //设置之后就不会创造新的Myjob实例
public class MyJob implements Job {

    /**
     * 秒杀商品的添加
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        String info = (String) jobDataMap.get("info");
        String size = (String) jobDataMap.get("size");
        Integer num = Integer.parseInt(size);
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getApplicationContext().getBean("redisTemplate");
        redisTemplate.opsForValue().set("seckill" + info, num, 60 * 60 * 24, TimeUnit.SECONDS);
    }
}
